import { loginByCode, logout, userMsg, loginByPwd } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    agentId: '',
    agentRole: '',
    userId: '',
    parentId: '',
    nickName: '',
    uroleKey: '',
    routeNo: '',
    routeData: {
      screenTitle: '',
      projectTitle: ''
    },
    userType: '',
    userInfo: {},
    enterpriseInfo: {},
    loginUserType: '',
    posArr: null,
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_AGENTID: (state, agentId) => {
      state.agentId = agentId
    },
    SET_AGENTROLE: (state, agentRole) => {
      state.agentRole = agentRole
    },
    SET_USERID: (state, userId) => {
      state.userId = userId
    },
    SET_PARENTID: (state, parentId) => {
      state.parentId = parentId
    },
    SET_NICKNAME: (state, nickName) => {
      state.nickName = nickName
    },
    SET_UROLEKEY: (state, uroleKey) => {
      state.uroleKey = uroleKey
    },
    SET_ROUTENO: (state, routeNo) => {
      state.routeNo = routeNo
    },
    SET_ROUTEDATA: (state, routeData) => {
      state.routeData = routeData
    },
    SET_USERTYPE: (state, userType) => {
      state.userType = userType
    },
    SET_ENTERPRISEINFO: (state, enterpriseInfo) => {
      state.enterpriseInfo = enterpriseInfo
    },
    SET_LOGINUSERTYPE: (state, userType) => {
      state.loginUserType = userType
    },
    SET_POSARR: (state, posArr) => {
      state.posArr = posArr
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo
    },
  },

  actions: {
    // 登录
    Login({ commit }, loginInfo) {
      return new Promise(async (resolve, reject) => {
        try {
          const { data: { token, user } } = loginInfo.phone
            ? await loginByCode(loginInfo)
            : await loginByPwd(loginInfo);
          setToken(token);
          commit('SET_TOKEN', token)
          commit('SET_USERINFO', user)
          resolve()
        } catch (error) {
          reject(error)
        }

      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise(async (resolve, reject) => {
        try {
          const { data } = await userMsg()
          commit('SET_USERINFO', data)
          resolve()
        } catch (error) {
          reject(error)
        }
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
